<?php

class ContentController extends C {

    function ContentController() {
        $this->textCss("common.css");
    }

    function nodes() {

        $this->assign("datas", getNode());
        $this->display("list.html");
    }

    function add() {
        if (!empty($_POST['post_title']) && $_GET['ac'] == 'saveAddNode') {
            $uid = $_SESSION['uid'] ? $_SESSION['uid'] : 1;
            $data = array(
                'title' => $_POST['post_title'],
                'content' => $_POST['post_content'],
                'cid' => $_POST['category'],
                'uid' => $uid,
                'weight' => $_POST['weight'],
                'status' => empty($_POST['status']) ? 0 : 1,
                'commend' => empty($_POST['commend']) ? 0 : 1,
                'created' => time()
            );
            createNode($data);
        }

        $this->assign("cateOptions", $this->getCategoryListInSelect(0, 0, $_GET['pid']));
        $this->display("add.html");
    }

    function edit() {
        if ($_GET['ac'] == 'saveEditNode' && !empty($_POST['post_title'])) {
            $uid = $_SESSION['uid'] ? $_SESSION['uid'] : 1;
            $data = array(
                'title' => $_POST['post_title'],
                'content' => $_POST['post_content'],
                'cid' => $_POST['category'],
                'uid' => $uid,
                'weight' => $_POST['weight'],
                'status' => empty($_POST['status']) ? 0 : 1,
                'commend' => empty($_POST['commend']) ? 0 : 1
            );
            editNode($_GET['nid'], $data);
        }

        $node = getNode($_GET['nid']);
        $this->assign("node", $node);
        $this->assign("cateOptions", $this->getCategoryListInSelect(0, 0, $node['cid']));
        $this->display();
    }

    //删除内容
    function delNode() {
        if ($_GET['ac'] == 'delnode' && $_GET['nid'] > 0) {
            $dbNode = new Csyr("node");
            $dbNode->where('nid=' . $_GET['nid'])->delete();
            redirect("/Content/nodes");
        }
    }

    //分类
    function category() {
        $this->assign("category", $this->getCategoryListInTable());
        $this->display();
    }

    function cateAdd() {

        //保存表单提交的数据
        if (isset($_POST['name']) && isset($_POST["pid"]) && $_GET['ac'] == 'savecategory') {

            $pid = empty($_POST['pid']) ? 0 : $_POST['pid'];
            $name = !empty($_POST['name']) ? $_POST['name'] : redirect("/Content/cateAdd/" . $this->P("pid=" . $pid), 2, "分类名称不能为空");
            $description = $_POST['description'];
            $ranking = $_POST['ranking'];
            $weight = $_POST['weight'];

            $data = array(
                'name' => $name,
                'pid' => $pid,
                'description' => $description,
                'ranking' => $ranking,
                'weight' => $weight,
                'created' => time()
            );
            $dbCate = new csyr("category");
            if ($dbCate->add($data)) {
                redirect("/Content/category", 2, "添加成功");
            } else {
                redirect("/Content/cateAdd/" . $this->P("pid=" . $pid), 2, "添加失败");
            }
        }

        $this->assign("cateOptions", $this->getCategoryListInSelect(0, 0, $_GET['pid']));

        $this->display("category-add");
    }

    function cateEdit() {

        //保存修改分类提交的表单数据
        if ($_GET['ac'] == 'savecateedit' && isset($_POST['name']) && isset($_GET['cid']) && isset($_POST['pid'])) {
            $pid = empty($_POST['pid']) ? 0 : $_POST['pid'];
            $name = !empty($_POST['name']) ? $_POST['name'] : redirect("/Content/cateAdd/" . $this->P("pid=" . $pid), 2, "分类名称不能为空");
            $description = $_POST['description'];
            $ranking = $_POST['ranking'];
            $weight = $_POST['weight'];

            $data = array(
                'name' => $name,
                'pid' => $pid,
                'description' => $description,
                'ranking' => $ranking,
                'weight' => $weight
            );
            $dbCate = new csyr("category");
            if ($dbCate->where("cid=" . $_GET['cid'])->save($data)) {
                redirect("/Content/category", 2, "修改成功");
            } else {
                redirect("/Content/cateEdit/" . $this->P("cid=" . $_GET['cid']), 2, "修改失败");
            }
        }

        //修改分类
        if (!isset($_GET['cid']) || empty($_GET['cid']))
            redirect("/Content/category", 3, "链接错误");

        $dbCate = new Csyr("category");
        $pid = $dbCate->getField("pid", "cid=" . $_GET['cid']);
        $this->assign("cateOptions", $this->getCategoryListInSelect(0, 0, $pid));

        $this->assign("cateRow", $dbCate->find($_GET['cid']));

        $this->display("category-edit");
    }

    function cateDel() {
        if (isset($_GET['cid']) && !empty($_GET['cid'])) {
            $dbCate = new Csyr("category");
            if ($dbCate->delete($_GET['cid']))
                redirect("/Content/category");
            else
                redirect("/Content/category", 2, '删除失败');
        }
    }

    function getCategoryListInTable($id = 0, $level = 0) {
        $db = new Csyr("category");
        $category_arr = $db->where("pid=" . $id)->order("ranking asc")->findall();
        if (!is_array($category_arr))
            return false;
        for ($lev = 0; $lev < $level + 1; $lev++) {
            $level_nbsp .= "　";
        }
        $level++;
        $level_nbsp .= "<font style=\"font-size:12px;font-family:wingdings\">" . $level . "</font>";
        foreach ($category_arr as $category) {
            $s.= "
			<tr class=\"tb-row\">
				<td height=\"26\" >" . $level_nbsp . " &nbsp; " . $category ['name'] . "&nbsp;&nbsp;(cid: " . $category ['cid'] . ")</td>
				<td height=\"26\" align=\"center\">" . $category ['ranking'] . "&nbsp;</td>
				<td height=\"26\" align=\"center\" class='operate'>
					<a href='" . $this->URL(array('class' => 'Content', 'function' => 'cateAdd', 'pid' => $category ['cid'])) . "'>添加子类</a> |
					<a href='" . $this->URL(array('class' => 'Content', 'function' => 'cateEdit', 'cid' => $category ['cid'])) . "'>修改</a> |
					<a href='" . $this->URL(array('class' => 'Content', 'function' => 'cateDel', 'cid' => $category ['cid'])) . "' onclick=\"return doAction('delete',{mes:'Are you sure to delete?'})\">删除</a>
				</td>
			</tr> ";
            $s.=$this->getCategoryListInTable($category ['cid'], $level);
        }
        return $s;
    }

    function getCategoryListInSelect($id=0, $level=0, $curId=Null) {
        $db = new Csyr("category");
        $category_arr = $db->where("pid=" . $id)->order("ranking asc")->findall();
        if (!is_array($category_arr))
            return false;
        for ($lev = 0; $lev < $level; $lev++) {
            $level_nbsp .= "&nbsp;";
        }
        $level++;
        $level_nbsp .= $level;
        foreach ($category_arr as $category) {
            $id = $category ['cid'];
            $name = $category ['name'];
            if ($id == $curId) {
                $selected = "selected";
            } else {
                $selected = "";
            }
            $s.= "<option $selected value='$id'>$level_nbsp$name</option>";

            $s.=$this->getCategoryListInSelect($id, $level, $curId);
        }
        return $s;
    }

}
